﻿<#
.SYNOPSIS  
    Searches SharePoint upgrade log files and extracts errors into an XML file that can be opened in Excel.  
.DESCRIPTION  
    
.NOTES
    
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Getting errors from upgrade log files ({0})..." -f [DateTime]::Now.ToString())

#get config
$config = [xml](Get-Content Upgrade.xml)

$logName = (Split-Path -Path $MyInvocation.MyCommand.Path -Parent) + "\Get-UpgradeErrors.xml"

Write-Host ("      Log file path: '{0}' ({1})..." -f $config.Settings.UpgradeLogFilePath, [DateTime]::Now.ToString())

$xtw = [System.Xml.XmlTextWriter]::Create($logName)
$xtw.WriteStartDocument($true)
$xtw.WriteStartElement("Matches")

$fileCollection = gci -Path $config.Settings.UpgradeLogFilePath -Filter "Upgrade-*"

foreach($fileName in $fileCollection){
	$fullPath = $config.Settings.UpgradeLogFilePath + "\" + $fileName
	$file = gc -Path $fullPath
	for($i=0;$i -le ($file.Count-1); $i++){
		if($file[$i] -match "\[ERROR\].*"){
			$xtw.WriteStartElement("Match")
			$xtw.WriteAttributeString("File", $fileName)
			$xtw.WriteAttributeString("LineNum", $i)
			$xtw.WriteString($Matches[0])
			$xtw.WriteEndElement()
		}
	}
}
$xtw.WriteEndElement()
$xtw.WriteEndDocument()
$xtw.Close()